#if 1

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/stddef.h>
#include <stdbool.h>

#define LOGFIFO "logfifo"
#define LOGFILE "/syslog.txt"

#define ERROR_EXIT(info) \
	{perror(info);return(-1);}

#define BUFMAXSIZE 4096
char buf[BUFMAXSIZE] = {0};


int main(int argc, char *argv[])
{
	bool logPrintToConsole = false;
	if(argc == 2)
	{
		printf("get input value :%c\n",*argv[1]);
		
		if(*argv[1] == '1')
		{
			logPrintToConsole = true;
		}else{
			logPrintToConsole = false;
		}
	}
	if(access(LOGFIFO,F_OK))/*判断是否已经创建了有名管道，如果已经创建，则返回0否则返回非0的数*/
	{
		if(mkfifo(LOGFIFO,0644) == -1)
		{
			ERROR_EXIT("mkfifo failed");
		}
	}

	int fifofd = open(LOGFIFO,O_RDONLY|O_NONBLOCK );
	if(fifofd == -1)
	{
		ERROR_EXIT("open fifo file error\n");
	}
	int logfd = open(LOGFILE,O_CREAT|  O_WRONLY |O_APPEND);
	if(logfd == -1)
	{
		ERROR_EXIT("open log file error\n");
	}
	
	printf("[%d]open logfifo :%s && logfile :%s success \n",getpid(),LOGFIFO,LOGFILE);
	
	fd_set set;
	FD_ZERO(&set);
	FD_SET(fifofd,&set);	
	while(1)
	{
		select(fifofd+1,&set,NULL,NULL,NULL);
		if(FD_ISSET(fifofd,&set))
		{
			int nread = read(fifofd,buf,BUFMAXSIZE-1);
			if(nread > 0)
			{
				buf[nread] = '\0';
				
				if(logPrintToConsole == true)
					printf("log%s\n",buf);
					
				write(logfd,buf,nread);
				
			}else if(nread == 0)
			{
				usleep(10*1000);
				continue;
			}else{
				ERROR_EXIT("read fifo error\n");
			}
		}

		usleep(10*1000);
	}
}

#endif

#if 0
/*
 * MKFIFO(3)                  Linux Programmer's Manual                 MKFIFO(3)

NAME
       mkfifo - make a FIFO special file (a named pipe)

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

extern int mkfifo(const char *pathname, mode_t mode);

#define handle_error(msg) \
    {perror(msg);exit(-1);}

#define logfifo "logfifo"

#define logfile "log1111.txt"

int main(void)
{
    printf(" log server !\n");
    if(access(logfifo,F_OK))/*判断是否已经创建了有名管道，如果已经创建，则返回0否则返回非0的数*/
    {
        int ret = mkfifo(logfifo,0777); /*创建有名管道,成功返回0,失败返回-1*/
        if(ret == -1) /*创建有名管道失败*/
            handle_error("make fifo failed :");
    }

    int fifofd  = open(logfifo, O_RDONLY);
    if(fifofd == -1)
        handle_error("open  fifo failed :");

    printf("open fifo success\n");

    int logfilefd = open(logfile, O_CREAT|O_WRONLY  | O_APPEND);
    if(logfilefd == -1)
        handle_error("open logfile failed :");


    int nread = 0;
    int fifobuf[1024] = {0};
    int size = 100;
    memset(fifobuf,0,1024);
    while(1)
    {
        nread = read(fifofd,fifobuf,size);
        if(nread == -1)
        {
            handle_error("read fifo error");
        }else if(nread>0)
        {
            printf("%s\n",fifobuf);
            if(write(logfilefd,fifobuf,nread) < 0)
            {
                 handle_error("write logfile error");
            }
        }else if(read == 0)
        {
                usleep (3);
                continue;
        }
        usleep(1);
    }
    close(logfile);
    close(fifofd);

    return 0;
}
#endif
